package com.wyl.salary.ui.page

import android.widget.Toast
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Done
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import com.wyl.salary.ui.components.AddSalary
import com.wyl.salary.ui.components.PreviewAddSalary
import com.wyl.salary.ui.theme.SalaryTheme
import com.wyl.salary.ui.viewmodel.AddSalaryViewModel

@Composable
fun AddSalaryPageWithViewModel(
    navController: NavHostController,
    viewModel: AddSalaryViewModel = viewModel(),
    onSalaryAdd: () -> Unit
) {
    val context = LocalContext.current
    val scrollState = rememberScrollState()
//    Log.d("Lele", "AddSalaryPageWithViewModel: addSalaryViewModel = $viewModel")
    AddSalaryPage(
        onSaveSalary = {
            viewModel.addSalary(onSuccess = {
                navController.popBackStack()
                onSalaryAdd()
            }) {
                Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
            }
        },
        onBackPress = { navController.popBackStack() },
        content = {
            AddSalary(
                date = viewModel.date,
                hours = viewModel.hours,
                salary = viewModel.salary,
                tipping = viewModel.tipping,
                remarks = viewModel.remarks,
                onDateChange = { viewModel.date = it },
                onHoursChange = { viewModel.hours = it },
                onSalaryChange = { viewModel.salary = it },
                onTippingChange = { viewModel.tipping = it },
                onRemarksChange = { viewModel.remarks = it },
                modifier = Modifier
                    .fillMaxHeight()
                    .verticalScroll(scrollState)
            )
        }
    )
}

@Composable
fun AddSalaryPage(
    onSaveSalary: () -> Unit,
    onBackPress: () -> Unit,
    content: @Composable () -> Unit,
) {
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        topBar = {
            TopAppBar(
                title = { Text("添加记录") },
                navigationIcon = {
                    IconButton(onClick = onBackPress) {
                        Icon(imageVector = Icons.Filled.ArrowBack, contentDescription = "返回")
                    }
                },
                actions = {
                    IconButton(onClick = onSaveSalary) {
                        Icon(imageVector = Icons.Filled.Done, contentDescription = "完成")
                    }
                }
            )
        },
        content = { content() }
    )
}


@Preview(showBackground = true)
@Composable
private fun PreviewAddSalaryPage() {
    SalaryTheme {
        AddSalaryPage(onSaveSalary = {},  onBackPress = {}) {
            PreviewAddSalary()
        }
    }
}



